#include "lgCommon.h"


extern unsigned char  c8563_Store[6] ;
#ifdef RTC_RX8010SJ

#define WRITE_CMD  0x64
#define READ_CMD   0x65

unsigned char    EEbuf[16];
unsigned char const nRunFlag[] = {15, 16};


//========================================================



///************************************
//** 函数原型: void IC_start(void);  **
//** 功    能: IC 总线起始位.         **
//************************************/
void I2C8010_start( void )
{
    GPIO_PinInit( RTC8010_SDA, GPIO_PinOutput );
    GPIO_PinInit( RTC8010_SCL, GPIO_PinOutput );
    RTC8010_SCL_HIGH;         /** Send Start **/
    RTC8010_SDA_HIGH;
    delay_us( 10 );
    RTC8010_SDA_LOW;
    delay_us( 10 );
    RTC8010_SCL_LOW;
}

////********************************** //、
//** 函数原型: void IC_stop(void); ** //
//** 功    能: IC 总线停止位.       ** //
//**********************************/
void I2C8010_stop( void )
{
    //  RTC_SCL_LOW;                                 /** Send Stop **/
    GPIO_PinInit( RTC8010_SDA, GPIO_PinOutput );
    RTC8010_SDA_LOW;
    delay_us( 10 );
    RTC8010_SCL_HIGH;
    delay_us( 10 );
    RTC8010_SDA_HIGH;

}


uint8_t  I2CCheckACK()
{
    int i = 0;
    GPIO_PinInit( RTC8010_SDA, GPIO_PinOutput );
    RTC8010_SDA_HIGH;
    delay_us( 10 );
    GPIO_PinInit( RTC8010_SDA, GPIO_PinInput ); //SDA设置为输入
    RTC8010_SCL_HIGH;
    while( RTC8010_SDA_IN )
    {
        i++;
        delay_us( 100 );
        if( i > 250 )
        {
            RTC8010_SCL_LOW;
            return 1;
        }
    }
    RTC8010_SCL_LOW;
    delay_us( 10);
    GPIO_PinInit( RTC8010_SDA, GPIO_PinOutput );
    return 0;
}

/////*************************************************** //
//** 函数原型: bit IC_writebyte(uchar wdata);       ** //
//** 功    能: 向 IC 总线发送 8 位数据,并请求一个应答信 ** //
//**           号 ACK.如果收到 ACK 应答则返回 1(TRUE),  ** //
//**           否则返回 0(FALSE).                    ** //
//***************************************************/
unsigned char IC_WriteByte( unsigned char wdata )
{
    unsigned char i;
    GPIO_PinInit( RTC8010_SDA, GPIO_PinOutput );
    for( i = 0; i < 8; i++ )
    {
        
        if( wdata & 0x80 )
        {
            RTC8010_SDA_HIGH;
        }
        else
        {
            RTC8010_SDA_LOW;
        }
        delay_us( 10 );
        RTC8010_SCL_HIGH;
        wdata <<= 1;
        delay_us( 10 );  
        RTC8010_SCL_LOW;

    }
    RTC8010_SCL_LOW;
    delay_us( 3 );
    if( I2CCheckACK() )
    {
        return( 0 );
    }
    else
    {
        return 1;
    }
}

/////*********************************************** //
//** 函数原型: uchar IC_readbyte(void);         ** //
//** 功    能: 从 IC 总线上接收 8 位数据,并将接受到 ** //
//**           8 位数据作为一个字节返回,不回送应 ** //
//**           答信号 ACK.                       ** //
//***********************************************/
unsigned char IC_ReadByte( void )
{
    unsigned char i;
    unsigned char IC_data = 0;
    GPIO_PinInit( RTC8010_SDA, GPIO_PinOutput );
    RTC8010_SCL_LOW;
    delay_us( 10 );
    RTC8010_SDA_HIGH;
    GPIO_PinInit( RTC8010_SDA, GPIO_PinInput );
    for( i = 0; i < 8; i++ )
    {

        RTC8010_SCL_HIGH;
        IC_data <<= 1;
        if( RTC8010_SDA_IN )
        {
            IC_data = IC_data | 0x01 ;
        }
        delay_us( 10 );
        RTC8010_SCL_LOW;
        delay_us( 10 );
    }


    RTC8010_SCL_HIGH;
    GPIO_PinInit( RTC8010_SDA, GPIO_PinOutput );
    RTC8010_SDA_LOW;
    delay_us( 10 );;
    RTC8010_SCL_LOW;
    return( IC_data );
}

/////*************************************************************** //
//** 函数原型: bit readEEone(uchar instr,uchar addr,uchar num); ** //
//** 功    能: 从 8010 中读取 num 个字节的数据,采用序列读操作方  ** //
//**           式从片内 Address 地址开始连续读取数据.8010 不接  ** //
//**           受指定的地址则返回 0(FALSE).                      ** //
//***************************************************************/
unsigned char readEEone( unsigned char addr, unsigned char num )
{
    unsigned char i;
    if( num <= 0 )
    {
        return 0;
    }
    I2C8010_start();
    if( IC_WriteByte( WRITE_CMD ) == 0 )
    {
        I2C8010_stop();
        return( 0 );
    }
    if( IC_WriteByte( addr ) == 0 )
    {
        I2C8010_stop();
        return( 0 );
    }
    I2C8010_start();
    if( IC_WriteByte( READ_CMD ) == 0 )
    {
        I2C8010_stop();
        return( 0 );
    }
    for( i = 0; i < num - 1; i++ )
    {
        EEbuf[i] = IC_ReadByte();
        GPIO_PinInit( RTC8010_SDA, GPIO_PinOutput );
        delay_us( 10 );;
        RTC8010_SDA_LOW;                             /** Send ACK **/
        delay_us( 10 );;
        RTC8010_SCL_HIGH;
    }
    EEbuf[num - 1] = IC_ReadByte();
    //       printf("value is %d \n",EEbuf[0]);
    GPIO_PinInit( RTC8010_SDA, GPIO_PinOutput );
    RTC8010_SDA_HIGH;      /** Send NACK **/
    delay_us( 10 );
    RTC8010_SCL_HIGH;
    I2C8010_stop();
    return( 1 );
}

///***************************************************************
//** 函数原型: bit writeEEone(uchar instr,uchar addr,uchar num);
//** 函数原型: bit writeEEone(uchar instr,uchar addr,uchar data);
//** 功    能:  写数字data 到 addr
//===================================================================
unsigned char writeEEone( unsigned char addr, unsigned char num )
{
    // unsigned char i;
    I2C8010_start();
    if( IC_WriteByte( WRITE_CMD ) == 0 )
    {
        I2C8010_stop();
        return( 0 );
    }
    if( IC_WriteByte( addr ) == 0 )
    {
        I2C8010_stop();
        return( 0 );
    }
    if( IC_WriteByte( num ) == 0 )
    {
        I2C8010_stop();
        return( 0 );
    }
    I2C8010_stop();
    delay_us( 2 );
    RTC8010_SDA_LOW;
    RTC8010_SCL_LOW;
    return( 1 );
}



void lgInitRTC()
{
  

    uint8_t nRes = lgTimeRead();
    if( nRes == 0 )
    {
         timeNow.nSecond = c8563_Store[0]; /*初始化时间*/
        timeNow.nMinute = c8563_Store[1]; /*初始化时间*/
        timeNow.nHour   = c8563_Store[2]; /*初始化时间*/
        timeNow.nDay    = c8563_Store[3]; /*初始化时间*/
        timeNow.nMonth  = c8563_Store[4]; /*初始化时间*/
        timeNow.nYear   = c8563_Store[5]; /*初始化时间*/
        lgSetTime();
    }
}



void  timbakrd( unsigned char* yhmhtim )
{
    unsigned char i;
    unsigned char res = 0;
    //  rtc_test();
    for( i = 0; i < 6; i++ )
    {
        res = readEEone( RX8010_TIME_BAK + i, 1 );
        if( res == 0 )
        {
            break;
        }
        *yhmhtim =  EEbuf[0];
        yhmhtim++;
    }
}

uint8_t  nTimeReg[6] = {RX8010_SEC,RX8010_MIN,RX8010_HOUR,RX8010_MDAY,RX8010_MONTH,RX8010_YEAR};

void lgSetTime()
{
    unsigned char i;
    int nRes ;
    uint8_t   nTimeBcd[6] = {0};
    uint8_t*  pTimeByte;
         
    timeNow.nSecond %= 60;
    timeNow.nMinute %= 60;    
    timeNow.nHour   %= 24;  
    
    if( timeNow.nMonth > 12 )
    {
        timeNow.nMonth = 1;
    }
    //day  1--31
    if(timeNow.nDay > 31 )
    {
        timeNow.nDay = 1;
    }
  
  
    pTimeByte = ( uint8_t* )&timeNow;
    for( i = 0; i < 6; i++ )
    {
        ResetWdg();
        nTimeBcd[i] =  hexbcd( *( pTimeByte + i ) );
        nRes = writeEEone( nTimeReg[i], nTimeBcd[i] );
        if( nRes == 0 )
        {
            //lastErrorx = -1;
        }
        if( i == 5 )
        {
            //存储的是年
            //year = *(yhmhtim+i);
        }
    }
}


//===============================================
/* WRITE  ：0x64 READ   ：0x65

writeEEone(0x1d,3);    写数据到 8010SJ  0X1D  是地址，3 是写入数据的数量。

要写入的数据存在 EEbuf[i] 中。 readEEone(0x1d,3);     读出数据到 EEbuf[i]

改变 SDA 方向： I2C_OUTPUT -----输出。 I2C_INPUT------输入*/
//================================================


unsigned char   lgTimeRead()
{
    unsigned   char res = 0;
    res = readEEone( RX8010_SEC, 1 );
    if( res == 0 ) //some error ，can not read second
    {
        return  0;
    }
    timeNow.nSecond =  bcdhex( EEbuf[0] );
    timeNow.nSecond %= 60;
        
    //  printf("second is %02x \n", EEbuf[0]);
    res = readEEone( RX8010_MIN, 1 );
    timeNow.nMinute =  bcdhex( EEbuf[0] );
    timeNow.nMinute %= 60;
        
    res = readEEone( RX8010_HOUR, 1 );
    timeNow.nHour =  bcdhex( EEbuf[0] );
    timeNow.nHour %= 24;
        
    res = readEEone( RX8010_MDAY, 1 );
    timeNow.nDay =  bcdhex( EEbuf[0] );
    if( timeNow.nDay == 0 )
    {
        timeNow.nDay = 1;
    }
    timeNow.nDay %= 32;
            
    res = readEEone( RX8010_MONTH, 1 );
    timeNow.nMonth =  bcdhex( EEbuf[0] );
    if( timeNow.nMonth == 0 )
    {
        timeNow.nMonth = 1;
    }
        
    timeNow.nMonth %= 13;
        
    res = readEEone( RX8010_YEAR, 1 );
    timeNow.nYear =  bcdhex( EEbuf[0] );
    return 1;
}

/*
void   TimeWrite( unsigned char* yhmhtim )
{
    if( yhmhtim == NULL )
    {
        writeEEone( RX8010_SEC, 0x00 );
        writeEEone( RX8010_MIN, 0x08 );
        writeEEone( RX8010_HOUR, 0x03 );
        writeEEone( RX8010_MDAY, 0x06 );
        writeEEone( RX8010_MONTH, 0x01 );
        writeEEone( RX8010_YEAR, 0x21 );
    }
    else
    {
        writeEEone( RX8010_SEC, *( yhmhtim ) );
        writeEEone( RX8010_MIN, *( yhmhtim + 1 ) );
        writeEEone( RX8010_HOUR, *( yhmhtim + 2 ) );
        writeEEone( RX8010_MDAY, *( yhmhtim + 3 ) );
        writeEEone( RX8010_MONTH, *( yhmhtim + 4 ) );
        writeEEone( RX8010_YEAR, *( yhmhtim + 5 ) );
    }
}
*/

void lgUpdateTime()
{

    static  unsigned char nClockCnt = 0;
    timeNow.nSecond += 1;
    if( timeNow.nSecond < 60 )
    {
        return;
    }
    timeNow.nSecond = 0;
    lgPowerTimeUpdate(); //停电时间更新
    //时钟累加
    timeNow.nMinute += 1;
    if( timeNow.nMinute < 60 )
    {
        return;
    }

        
    lgTimeRead(); //每24小时校正时钟一次
}


#endif
